using System;
using System.Runtime.CompilerServices;
using Nsb.Classes;

namespace MinionsEdit.Classes
{
    [IgnoreNamespace]
    public class NColor
    {
        public double Alpha = 1;
        public int Blue;
        public int Green;
        public int Red;

        public string ToRgba()
        {
            return String.Format("rgba({0}, {1}, {2}, {3})", Red, Green, Blue, Alpha);
        }

        public string ToRgb()
        {
            return String.Format("rgb({0}, {1}, {2})", Red, Green, Blue);
        }

        public NColor R(int c)
        {
            Red = c;
            return this;
        }

        public NColor G(int c)
        {
            Green = c;
            return this;
        }

        public NColor B(int c)
        {
            Blue = c;
            return this;
        }

        public NColor A(double a)
        {
            Alpha = a;
            return this;
        }
        public NColor Parse(string c)
        {
            c = c.Trim();
            string[] tc = new string[] {"0", "0", "0", "1"};
            try
            {
                if (c.IndexOf("rgba") == 0)
                    tc = (string[])Script.Eval(c.ReplaceRegex((RegularExpression)Script.Literal(@"/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*([.\d]+)\)$/"), "['$1','$2','$3','$4']"));
                else if (c.IndexOf("rgb") == 0)
                    tc = (string[])Script.Eval(c.ReplaceRegex((RegularExpression)Script.Literal(@"/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/"), "['$1','$2','$3','1']"));
                else if (c.IndexOf("#") == 0)
                {
                    if (c.Length == 7)
                        tc = (string[])Script.Eval(c.ReplaceRegex((RegularExpression)Script.Literal(@"/^#([\dA-F][\dA-F])([\dA-F][\dA-F])([\dA-F][\dA-F])$/i"), "['$1','$2','$3','1']"));
                    else if (c.Length == 4)
                        tc = (string[])Script.Eval(c.ReplaceRegex((RegularExpression)Script.Literal(@"/^#([\dA-F])([\dA-F])([\dA-F])$/"), "['$1$1','$2$2','$3$3','1']"));
                    else if (c.Length == 9)
                    {
                        tc = (string[])Script.Eval(c.ReplaceRegex((RegularExpression)Script.Literal(@"/^#([\dA-F][\dA-F])([\dA-F][\dA-F])([\dA-F][\dA-F])([\dA-F][\dA-F])$/i"), "['$2','$3','$4','$1']"));
                        tc[3] = (100 / Number.ParseInt(tc[3],16)).ToString().Substr(0,4);
                    }
                    else if (c.Length == 5)
                    {
                        tc = (string[])Script.Eval(c.ReplaceRegex((RegularExpression)Script.Literal(@"/^#([\dA-F])([\dA-F])([\dA-F])([\dA-F])$/i"), "['$2$2','$3$3','$4$4','$1$1']"));
                        tc[3] = (100 / Number.ParseInt(tc[3],16)).ToString().Substr(0,4);
                    }
                }
            }
            catch (Exception ex)
            {
                Inform.Debug("Failed to convert color {0} : {1}", c, ex);
            }

            int radix = c.Substr(0, 1) == "#" ? 16 : 10;
            Red = Number.ParseInt(tc[0], radix);
            Green = Number.ParseInt(tc[1], radix);
            Blue = Number.ParseInt(tc[2], radix);
            Alpha = Number.ParseDouble(tc[3]);
            return this;
        }
    }
}